package com.vrv.mysqlback;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

/**
 * @ClassName: DefaultMysqldumpLocator
 * @Desc: TODO
 * @Auther: <Bang>
 * @Date: 2021/08/27 11:21
 * @Version: 1.0
 * @Modified By:
 */
public class DefaultMysqldumpLocator extends MysqldumpLocator{

    private static final Logger log = LoggerFactory.getLogger(DefaultMysqldumpLocator.class);

        /**
         * Trace the version of the bundled mysqldump executable. It's a counter: every
         * time the bundled mysqldump change it is incremented by 1.
         */
        private static final int MYEX_EVERSION = 1;


        /**
         * The mysqldump executable file path.
         */
        private String path;

        /**
         * It builds the default mysqldumpLocator, exporting the mysqldump executable on a
         * temp file.
         */
        public DefaultMysqldumpLocator() {
            // Windows?
            boolean isWindows=OperatingSystemUtils.isWindows();
            // Temp dir?
            File temp = new File(System.getProperty("java.io.tmpdir"), "mysqldump-"
                    + MYEX_EVERSION);
            if (!temp.exists()) {
                temp.mkdirs();
                temp.deleteOnExit();
            }
            // mysqldump executable export on disk.
            String suffix = isWindows ? ".exe" : "";
            File exe = new File(temp, "mysqldump" + suffix);
            if (!exe.exists()) {
                copyFile("bin/mysqldump" + suffix, exe);
            }

            // Need a chmod?
            if (!isWindows) {
                Runtime runtime = Runtime.getRuntime();
                try {
                    runtime.exec(new String[] { "/bin/chmod", "755",
                            exe.getAbsolutePath() });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            // Ok.
            this.path = exe.getAbsolutePath();
        }



    /**
     * 获取mysqldump执行路径
     *
     * @return
     */
    @Override
    protected String getMysqldumpExecutablePath() {
        return path;
    }

    /**
         * Copies a file bundled in the package to the supplied destination.
         *
         * @param path The name of the bundled file.
         * @param dest The destination.
         * @throws RuntimeException If aun unexpected error occurs.
         */
        private void copyFile(String path, File dest) throws RuntimeException {
            InputStream input = null;
            OutputStream output = null;
            try {
                input = getClass().getClassLoader().getResourceAsStream(path);
                output = new FileOutputStream(dest);
                byte[] buffer = new byte[1024];
                int l;
                while ((l = input.read(buffer)) != -1) {
                    output.write(buffer, 0, l);
                }
            } catch (IOException e) {
                throw new RuntimeException("Cannot write file "
                        + dest.getAbsolutePath());
            } finally {
                if (output != null) {
                    try {
                        output.close();
                    } catch (Throwable ignored) {

                    }
                }
                if (input != null) {
                    try {
                        input.close();
                    } catch (Throwable ignored) {
                    }
                }
            }
            if (!dest.exists()) {
                String errMsg = "copy mysqldump executable file to " + dest.getAbsolutePath() + " fail";
                log.info(errMsg);
                throw new IllegalStateException(errMsg);
            }
        }
}
